package com.frxuexi.shop2307.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.room.SignEntity
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.SignAdapter
import com.frxuexi.shop2307.adapter.SignScoreAdapter
import com.frxuexi.shop2307.databinding.ActivitySignBinding
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@Route(path = "/app/SignActivity")
@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding,SignViewModel>() {
    val signAdapter by lazy { SignAdapter() }
    val signScoreAdapter by lazy { SignScoreAdapter() }
    var pos=0
    var sum=0
    lateinit var entity: SignEntity
    val list = listOf<SignEntity>(
        SignEntity(1, "", "第一天", 10,false),
        SignEntity(2, "", "第二天", 20,false),
        SignEntity(3, "", "第三天", 30,false),
        SignEntity(4, "", "第四天", 40,false),
        SignEntity(5, "", "第五天", 50,false),
        SignEntity(6, "", "第六天", 60,false),
        SignEntity(7, "", "第七天", 70,false)
    )
    override fun initData() {
        vm.addSignData.observe(this){
            signAdapter.notifyItemChanged(pos)
//            ToastUtils.showLong(it.toString())
//            sum+=entity.score
//            binding.signSum.setText("积分：${sum}")
//            binding.signDay.setText(entity.id.toString())
            vm.selectSign()
        }

        vm.selectSignData.observe(this){
            signScoreAdapter.apply {
                data.clear()
                addData(it)
                notifyDataSetChanged()
            }
            sum=0
            for (i in 0..it.size-1){
                val signEntity = signAdapter.data.get(i)
                signEntity.isSign=true
                sum+=signEntity.score
            }
            signAdapter.notifyDataSetChanged()
            binding.signDay.setText(it.size.toString())
            binding.signSum.setText("积分：${sum}")
        }

    }

    override fun initView() {

        signAdapter.addData(list)
        binding.signRvDay.apply {
            adapter=signAdapter
            layoutManager=GridLayoutManager(this@SignActivity,7)
        }

        binding.signRv.apply {
            adapter=signScoreAdapter
            layoutManager=LinearLayoutManager(this@SignActivity)
        }

        vm.selectSign()

        signAdapter.setOnItemClickListener { adapter, view, position ->
            pos=position
            entity = list.get(position)
            entity.time=SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
            entity.isSign=true
            vm.addSign(entity)
        }

        binding.signAdd.setOnClickListener {
            pos=binding.signDay.text.toString().toInt()
            entity = list.get(pos)
            entity.time=SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
            entity.isSign=true
            vm.addSign(entity)
        }

    }

}